/**
 *
 * Module        : Module Size
 * Authors       : Ben Kwint & Arie van der Veek
 * Date          : November 7th 2011
 * Description   : This module provides the application the the possibility
 *                 to detect whether a line of code is actually executable code
 *                 or just comment. It both works for single line comment and multi
 *                 line comment.
 *
 */
module Regular::CommentLines

import List;
import IO;

/**
 *
 * Function that allows you to call isSingleCommentLine without the boolean
 * parameter that determins whether there is code before
 *
 */
public bool isSingleCommentLine(str line)
{
  return isSingleCommentLine(line, false);
}

/*
 *
 * Determins whether the line of code contains comments
 *
 * In the case of no code before everything is fine, in the case
 * that you want to be able to detect whether there is comment behind
 * the code you first have to detect whether there is an even number of 
 * quotes before the comment, else the comment is in a string
 *
 */
public bool isSingleCommentLine(str line, bool code)
{
  if( code )
  {
    return (/^.*\/\/.*/ := line);
  }
  else
  {
    return (/^[ \t]*\/\/.*/ := line);
  }
} 

/**
 *
 * Get the number of single comment lines, also counts the comments that
 * are prepended behind code
 *
 * Needs some work to delete the cases where the // is within quotes, see
 * the definition of isSingleCommentLine
 *
 */
public int SingleCommentLines(list[str] lines)
{
  int nrOfComments = 0;
  int s = size(lines)-1;
  for(int n <- [0..s])
  {
    nrOfComments += (isSingleCommentLine(lines[n], true)) ? 1 : 0;
  }
  return nrOfComments;
}

/**
 *
 * Gets the comment lines where there is no code on the comment line
 *
 */
public int MultiLineComment(list[str] lines)
{
  return MultiLineComment(lines, false);
}


/**
 *
 * Determins the number of multiline comment lines in the given file
 * the boolean parameter code sets whether you want to count lines that 
 * have a start or end sign but also have code as a comment line
 *
 */
public int MultiLineComment(list[str] lines, bool code)
{
  int _start = -1;
  int _end = -1;
  
  int nrOfLines = 0;
  
  for(n <- [0..(size(lines)-1)])
  {
    if( _start == -1 && (/^(.*)\/\*.*/ := lines[n]))
    {
      _start = n;
    }
    else if((/^(.*)\*\/.*/ := lines[n]) && _start != -1)
    {
      _end = n;
      nrOfLines += (_end-_start+1);
        
      if( !code )
      {
        nrOfLines -= (/^([ \t]*)\/\*.*/ := lines[_start]) ? 0 : 1;
        nrOfLines -= (/^(.*)\*\/([ \t])*$/ := lines[_end]) ? 0 : 1;
      }
      
      _start = -1;
      _end = -1;
    }
  }
  
  return nrOfLines;
}









